/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.interpreter.frontends.functional;

/**
 *
 * @author Laurent WOUTERS
 */
public class AnnotationLoader {
    public static void loadAnnotations(xowl.lang.owl2.Annotation_OR_Axiom_OR_Ontology annotated, Loader loader, FElementNode node, xowl.interpreter.frontends.LexicalContext context) throws FParserException {
        for (int i=0; i!=node.getChildren().size(); i++) {
            FNode child = node.getChildren().get(i);
            if (child.getType() != FNodeType.Element)
                return;
            FElementNode elem = (FElementNode)child;
            if (!xowl.interpreter.frontends.VocOWL2.ontoAnnotation.equals(elem.getName()))
                return;
            annotated.addAnnotations(load(loader, elem, context));
            node.getChildren().remove(0);
            i--;
        }
    }

    public static xowl.lang.owl2.Annotation load(Loader loader, FElementNode node, xowl.interpreter.frontends.LexicalContext context) throws FParserException {
        xowl.lang.owl2.Annotation annotation = new xowl.lang.owl2.Annotation();
        loadAnnotations(annotation, loader, node, context);
        annotation.setAnnotProperty(loadAnnotationProperty(loader, node.getChildren().get(0), context));
        annotation.setAnnotValue(loadAnnotationValue(loader, node.getChildren().get(1), context));
        return annotation;
    }

    public static xowl.lang.owl2.IRI loadAnnotationProperty(Loader loader, FNode node, xowl.interpreter.frontends.LexicalContext context) throws FParserException {
        return loader.getIRI(((FIRINode)node).getIRI());
    }
    public static xowl.lang.owl2.IRI loadAnnotationIRI(Loader loader, FNode node, xowl.interpreter.frontends.LexicalContext context) throws FParserException {
        return loader.getIRI(((FIRINode)node).getIRI());
    }
    public static xowl.lang.owl2.AnnotationSubject loadAnnotationSubject(Loader loader, FNode node, xowl.interpreter.frontends.LexicalContext context) throws FParserException {
        if (node.getType() == FNodeType.IRI)
            return loadAnnotationIRI(loader, node, context);
        if (node.getType() == FNodeType.NodeID)
            return ExpressionLoader.load_AnonymousIndividual(loader, (FNodeIDNode)node, context);
        return null;
    }
    public static xowl.lang.owl2.AnnotationValue loadAnnotationValue(Loader loader, FNode node, xowl.interpreter.frontends.LexicalContext context) throws FParserException {
        if (node.getType() == FNodeType.IRI)
            return loadAnnotationIRI(loader, node, context);
        if (node.getType() == FNodeType.NodeID)
            return ExpressionLoader.load_AnonymousIndividual(loader, (FNodeIDNode)node, context);
        if (node.getType() == FNodeType.Literal)
            return ExpressionLoader.load_Literal(loader, (FLiteralNode)node, context);
        return null;
    }
}
